﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.Enhance.ThreadTool;
using CatUtils.Utils.Helper;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;
using RabbitDownload.Helper;
using RabbitDownload.Helper.Currency;

using RabbitDownload.Helper.Enhance;
using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.Pixiv.Successive
{
    internal class SuccessiveHelper
    {/// <summary>
     /// 缓存记录的图片ID
     /// </summary>
        public static string TempProgress = StaticValue.BinPath + "SucessiveProgress.txt";

        /// <summary>
        /// 缓存扫描过的图片信息
        /// </summary>
        public string TempDownloadImgInfo = StaticValueOld.SaveListPath + "ImgInfoTemp\\";

        public static void StartScanner(int StartId, int EndID, int Score)
        {
            String SaveListPath = StaticValueOld.SaveListPath + string.Format("PixivIdText{0}TO{1}.txt", StartId, EndID);
            FloderHelper.FloderExits(StaticValueOld.SaveListPath, true);
            FloderHelper.FloderExits(new SuccessiveHelper().TempDownloadImgInfo, true);
            Console.WriteLine("保存路径" + SaveListPath);
            for (; StartId < EndID; StartId++)
            {
                ThreadSleep.SetSecondClock(5);

                FileHelper.WriteText(TempProgress, StartId.ToString());
                string ImgUrl = AnalysisPage(StartId, Score);
                if (ImgUrl != "")

                    FileHelper.AppendText(SaveListPath, ImgUrl);
            }
        }

        private static string AnalysisPage(int ImgId, int Score)
        {
            int ImgScore = 0;

            ///require-register medium-image  _work
            String ImgPageurl = "https://www.pixiv.net/member_illust.php?mode=medium&illust_id=" + ImgId;

            String Shtml = WebHelper.HttpPcGet(ImgPageurl);
            ImgScore = TextHelper.StringToInt(
                HtmlHelper.GetFirstText(Shtml, "//ul/li[@class='info'][2]/span[@class='views']")
                );
            Console.WriteLine("解析图片ID{0}，图片分数为{1}", ImgId, ImgScore);
            if (ImgScore < Score)
                return "";
            String SingleImgUrl = HtmlHelper.GetFirstSrc(Shtml, "//a[@class='require-register medium-image  _work ']/img");
            if (SingleImgUrl == "")
            {
                Console.WriteLine("画集");
                SingleImgUrl = HtmlHelper.GetFirstSrc(Shtml, "//a[@class='medium-image  _work multiple ']/img");
            }
            if (SingleImgUrl == "")//gif和空指针不再寻找
                return "";
            Console.WriteLine("图片链接：" + SingleImgUrl);
            List<string> Taglist = new List<string>() { SingleImgUrl };
            Taglist.AddRange(HtmlHelper.GetTextValueList(Shtml, "//ul[@class='inline-list']/li/a[2]"));
            FileHelper.WriteList(new SuccessiveHelper().TempDownloadImgInfo + string.Format("{0}ImgScore{1}.txt", ImgId, ImgScore), Taglist);
            return SingleImgUrl;
        }
    }
}